package com.joysuch.wwyt.risk.util;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFDataValidation;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Author ZhaoQiuWei
 * @Date 2022/2/17 9:32
 */
@Component
public class RiskEvalExcelUtil {

//    @Autowired
//    private static RiskPointDao riskPointDao;
//
//    @Autowired
//    private static RiskPointService riskPointService;
//
//    @Autowired
//    private static RiskPointTypeDao riskPointTypeDao;
//
//    @Autowired
//    private static BaseUserDao baseUserDao;


    /**
     * 解决下拉框过长不显示问题
     *
     * @param workbook
     * @param list       下拉数据数组
     * @param sheet
     * @param firstRow   开始行
     * @param endRow     结束行
     * @param cellNum    下拉框所在的列
     * @param sheetIndex 隐藏sheet名称
     */
    public static void getSelectData(SXSSFWorkbook workbook, List<String> list, SXSSFSheet sheet, int firstRow, int endRow, int cellNum, int sheetIndex) {
        String hiddenName = "hidden" + sheetIndex + "_" + cellNum;
        //+"_"+ RandomUtil.randomString(4)
        //1.创建隐藏的sheet页
        SXSSFSheet hidden = workbook.createSheet(hiddenName);
        //2.循环赋值（为了防止下拉框的行数与隐藏域的行数相对应，将隐藏域加到结束行之后）
        for (int i = 0; i < list.size(); i++) {
            hidden.createRow(endRow + i).createCell(cellNum).setCellValue(list.get(i));
        }
        Name category1Name = workbook.createName();
        category1Name.setNameName(hiddenName);
        //3 A1:A代表隐藏域创建第N列createCell(N)时。以A1列开始A行数据获取下拉数组
        category1Name.setRefersToFormula(hiddenName + "!A1:A" + (list.size() + endRow));
        //
        DataValidationHelper helper = sheet.getDataValidationHelper();
        DataValidationConstraint constraint = helper.createFormulaListConstraint(hiddenName);
        CellRangeAddressList addressList = new CellRangeAddressList(firstRow, endRow, cellNum, cellNum);
        DataValidation dataValidation = helper.createValidation(constraint, addressList);
        if (dataValidation instanceof XSSFDataValidation) {
            // 数据校验
            dataValidation.setSuppressDropDownArrow(true);
            dataValidation.setShowErrorBox(true);
        } else {
            dataValidation.setSuppressDropDownArrow(false);
        }
        // 作用在目标sheet上
        sheet.addValidationData(dataValidation);
        // 设置hiddenSheet隐藏
        workbook.setSheetHidden(sheetIndex, true);
    }


    public static String getFloatDecimal(Float f) {
        // 设置位数
        int scale = 2;
        // 四舍五入
        int roundingMode = 4;
        BigDecimal bd = new BigDecimal((float) f);
        bd = bd.setScale(scale, roundingMode);
        f = bd.floatValue();
        return f + "";
    }


    //小标题的样式
    public static CellStyle title(Workbook wb, Boolean isRed, int height) {
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("黑体");
        font.setFontHeightInPoints((short) height); //12
        if (isRed) {
            font.setColor((short) 2); //红2
        }
        style.setFont(font);
        style.setAlignment(HorizontalAlignment.CENTER);                //横向居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);     //纵向居中
        return style;
    }

    //文字样式
    public static CellStyle text(Workbook wb, int height, boolean isRed) {
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("Times New Roman");
        font.setFontHeightInPoints((short) height);//10
        if (isRed) {
            font.setColor((short) 2); //红2
        }
        style.setFont(font);
        style.setAlignment(HorizontalAlignment.LEFT);              //横向居左
        style.setVerticalAlignment(VerticalAlignment.CENTER);     //纵向居中
        return style;
    }

}
